package com.zhb.cloud.core.distribute.lock;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName: DistributedLock
 * @author: zhb
 * @date: 2017年8月3日 下午7:05:45
 * @Description: 核心接口
 *
 */
public interface DistributedLock {
	/**
	 * @Title: lock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:36
	 * @Description: 直接加锁
	 * @throws DistributedLockException
	 */
	void lock() throws DistributedLockException;

	/**
	 * @Title: lock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:41
	 * @Description: 锁定多长时间
	 * @param leaseTime
	 * @param unit
	 * @throws DistributedLockException
	 */
	void lock(long leaseTime, TimeUnit unit) throws DistributedLockException;

	/**
	 * @Title: unlock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:48
	 * @Description: 释放所
	 * @throws DistributedLockException
	 */
	void unlock() throws DistributedLockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:52
	 * @Description: Acquires the lock only if it is not held by another process
	 *               at the time of invocation.
	 * @return
	 * @throws DistributedLockException
	 */
	boolean tryLock() throws DistributedLockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:31:57
	 * @Description: Acquires the lock if it is not held by another thread
	 *               within the given waiting time and the current thread has
	 *               not been interrupted.
	 * @param waitTime
	 * @param unit
	 * @return
	 * @throws DistributedLockException
	 */
	boolean tryLock(long waitTime, TimeUnit unit) throws DistributedLockException;

	/**
	 * @Title: tryLock
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:32:01
	 * @Description: Returns <code>true</code> as soon as the lock is acquired.
	 *               If the lock is currently held by another thread in this or
	 *               any other process in the distributed system this method
	 *               keeps trying to acquire the lock for up to
	 *               <code>waitTime</code> before giving up and returning
	 *               <code>false</code>. If the lock is acquired, it is held
	 *               until <code>unlock</code> is invoked, or until
	 *               <code>leaseTime</code> have passed since the lock was
	 *               granted - whichever comes first.
	 * @param waitTime
	 * @param leaseTime
	 * @param unit
	 * @return
	 */
	boolean tryLock(long waitTime, long leaseTime, TimeUnit unit);

	/**
	 * @Title: isLocked
	 * @author: zhb
	 * @date: 2017年8月4日 上午10:32:06
	 * @Description: 判断是否锁定
	 * @return
	 * @throws DistributedLockException
	 */
	boolean isLocked() throws DistributedLockException;
}
